package com.example.administrator.caigou51.widget.dialog;

import android.os.CountDownTimer;
import android.view.View;

import com.ab.util.AbLogUtil;
import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.activity.GBaseActivity;


import cn.pedant.SweetAlert.SweetAlertDialog;
import cn.pedant.SweetAlert.SweetAlertDialog.OnSweetClickListener;

public class SweetDialogManager {
    private int i = -1;
    private SweetAlertDialog dialog;
    private static SweetDialogManager instance;
    //处理成功，request先不取消对话框
    private boolean successNotCancel;

    public boolean isSuccessNotCancel() {
        return successNotCancel;
    }

    public void setSuccessNotCancel(boolean successNotCancel) {
        this.successNotCancel = successNotCancel;
    }

    public static SweetDialogManager getInstance() {
        if (instance == null) {
            instance = new SweetDialogManager();
        }
        return instance;
    }

    /**
     * 一句话+一个确认按钮
     *
     * @param activity
     * @param contentText
     * @param cancelable
     * @param touchOutSideToCancel
     */
    public void showDialogByBasicText(GBaseActivity activity, String contentText, boolean cancelable, boolean touchOutSideToCancel) {
        if (activity == null) return;
        if (dialog != null && dialog.isShowing()) {
            // progressDlg.setMsg(sMsg);
        } else {
            if (!activity.isFinishing() && !activity.isOnStopActivity) {
                dialog = new SweetAlertDialog(activity);
                dialog.setCancelable(cancelable);
                dialog.setTitleText(contentText);
                dialog.setCanceledOnTouchOutside(touchOutSideToCancel);
                dialog.show();
            }
        }
    }

    /**
     * 一个标题+一句话+一个确认按钮
     *
     * @param activity
     * @param titleText
     * @param contentText
     * @param cancelable
     * @param touchOutSideToCancel
     */
    public void showDialogByTitleText(GBaseActivity activity, String titleText, String contentText, boolean cancelable, boolean touchOutSideToCancel) {
        if (activity == null) return;
        if (dialog != null && dialog.isShowing()) {
            // progressDlg.setMsg(sMsg);
        } else {
            if (!activity.isFinishing() && !activity.isOnStopActivity) {
                dialog = new SweetAlertDialog(activity);
                dialog.setCancelable(cancelable);
                dialog.setTitleText(titleText);
                dialog.setContentText(contentText);
                dialog.setCanceledOnTouchOutside(touchOutSideToCancel);
                dialog.show();
            }
        }


    }

    /**
     * 一个错误图标+一个标题+一句话+一个确认按钮
     *
     * @param activity
     * @param titleText
     * @param cancelable
     * @param touchOutSideToCancel
     */
    public void showDialogByErrorTitleText(GBaseActivity activity, String titleText, boolean cancelable, boolean touchOutSideToCancel) {
        if (activity == null) return;

        if (dialog != null && dialog.isShowing()) {
            dialog.setCancelable(cancelable);
            dialog.setTitleText(titleText);
            dialog.setCanceledOnTouchOutside(touchOutSideToCancel);
            dialog.setConfirmText("OK");
            dialog.changeAlertType(SweetAlertDialog.ERROR_TYPE);
        } else {
            if (dialog != null && dialog.isShowing()) {
                // progressDlg.setMsg(sMsg);
            } else {
                if (!activity.isFinishing() && !activity.isOnStopActivity) {
                    dialog = new SweetAlertDialog(activity, SweetAlertDialog.ERROR_TYPE);
                    dialog.setCancelable(cancelable);
                    dialog.setTitleText(titleText);
                    dialog.setContentText("");
                    dialog.setCanceledOnTouchOutside(touchOutSideToCancel);
                    dialog.show();
                }
            }
        }

    }
    /**
     * 一个正确图标+一个标题+一句话+2个确认按钮
     *
     * @param activity
     * @param titleText
     * @param contentText
     * @param cancelable
     * @param touchOutSideToCancel
     */
    public void showDialogBySuccessTitleTextTwoButton(GBaseActivity activity, String titleText, String contentText, boolean cancelable, boolean touchOutSideToCancel, OnSweetClickListener onSweetClickListener, OnSweetClickListener onCancelSweetClickListener,String cancel,String sure) {
        if (activity == null) return;
        if (dialog != null && dialog.isShowing()) {
            dialog.setCancelable(cancelable);
            dialog.setTitleText(titleText);
            dialog.setContentText(contentText);
            dialog.setCanceledOnTouchOutside(touchOutSideToCancel);

            dialog.setCancelText(cancel);
            dialog.setConfirmText(sure);
            dialog.setCanceledOnTouchOutside(touchOutSideToCancel);
            dialog.setCancelClickListener(onCancelSweetClickListener);

            dialog.setConfirmClickListener(onSweetClickListener);
            dialog.changeAlertType(SweetAlertDialog.SUCCESS_TYPE);

        } else {
            if (dialog != null && dialog.isShowing()) {
                // progressDlg.setMsg(sMsg);
            } else {
                if (!activity.isFinishing() && !activity.isOnStopActivity) {
                    dialog = new SweetAlertDialog(activity, SweetAlertDialog.SUCCESS_TYPE);
                    dialog.setCancelable(cancelable);
                    dialog.setTitleText(titleText);
                    dialog.setContentText(contentText);
                    dialog.setCanceledOnTouchOutside(touchOutSideToCancel);
                    dialog.setCancelText(cancel);
                    dialog.setConfirmText(sure);
                    dialog.setCancelClickListener(onCancelSweetClickListener);
                    dialog.setConfirmClickListener(onSweetClickListener);
                    dialog.show();
                }
            }
        }

    }
    /**
     * 一个正确图标+一个标题+一句话+一个确认按钮
     *
     * @param activity
     * @param titleText
     * @param contentText
     * @param cancelable
     * @param touchOutSideToCancel
     */
    public void showDialogBySuccessTitleText(GBaseActivity activity, String titleText, String contentText, boolean cancelable, boolean touchOutSideToCancel, OnSweetClickListener onSweetClickListener) {
        if (activity == null) return;
        if (dialog != null && dialog.isShowing()) {
            dialog.setCancelable(cancelable);
            dialog.setTitleText(titleText);
            dialog.setContentText(contentText);
            dialog.setCanceledOnTouchOutside(touchOutSideToCancel);
            dialog.setConfirmClickListener(onSweetClickListener);
            dialog.changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
        } else {
            if (dialog != null && dialog.isShowing()) {
                // progressDlg.setMsg(sMsg);
            } else {
                if (!activity.isFinishing() && !activity.isOnStopActivity) {
                    dialog = new SweetAlertDialog(activity, SweetAlertDialog.SUCCESS_TYPE);
                    dialog.setCancelable(cancelable);
                    dialog.setTitleText(titleText);
                    dialog.setContentText(contentText);
                    dialog.setCanceledOnTouchOutside(touchOutSideToCancel);
                    dialog.setConfirmClickListener(onSweetClickListener);
                    dialog.show();
                }
            }
        }

    }

    /**
     * 一个警告图标+一个标题+一句话+一个确认按钮+取消确认事件
     *
     * @param activity
     * @param titleText
     * @param contentText
     * @param cancelable
     * @param touchOutSideToCancel
     * @param cancelListener
     * @param confirmListener
     */
    public void showDialogByWarmTitleText(GBaseActivity activity, String titleText, String contentText, boolean cancelable, boolean touchOutSideToCancel, OnSweetClickListener cancelListener, OnSweetClickListener confirmListener) {
        if (activity == null) return;
        if (dialog != null && dialog.isShowing()) {
            // progressDlg.setMsg(sMsg);
        } else {
            if (!activity.isFinishing() && !activity.isOnStopActivity) {
                dialog = new SweetAlertDialog(activity, SweetAlertDialog.WARNING_TYPE);
                dialog.setCancelable(cancelable);
                dialog.setTitleText(titleText);
                dialog.setContentText(contentText);
                dialog.setCancelText("取消");
                dialog.setConfirmText("确定");
                dialog.setCanceledOnTouchOutside(touchOutSideToCancel);
                dialog.setCancelClickListener(cancelListener);
                dialog.setConfirmClickListener(confirmListener);
                dialog.show();
            }
        }


    }

    /**
     * 一个警告图标+一个标题+一句话+自定义按钮文字和事件
     *
     * @param activity
     * @param titleText
     * @param contentText
     * @param cancelable
     * @param touchOutSideToCancel
     * @param cancelListener
     * @param confirmListener
     */
    public void showDialogByWarmTitleText(GBaseActivity activity, String titleText, String contentText, boolean cancelable, boolean touchOutSideToCancel, OnSweetClickListener cancelListener, OnSweetClickListener confirmListener, String CancelText, String ConfirmText) {
        if (activity == null) return;
        if (dialog != null && dialog.isShowing()) {
            // progressDlg.setMsg(sMsg);
        } else {
            if (!activity.isFinishing() && !activity.isOnStopActivity) {
                dialog = new SweetAlertDialog(activity, SweetAlertDialog.WARNING_TYPE);
                dialog.setCancelable(cancelable);
                dialog.setTitleText(titleText);
                dialog.setContentText(contentText);
                dialog.setCancelText(CancelText);
                dialog.setConfirmText(ConfirmText);
                dialog.setCanceledOnTouchOutside(touchOutSideToCancel);
                dialog.setCancelClickListener(cancelListener);
                dialog.setConfirmClickListener(confirmListener);
                dialog.show();
            }
        }

    }

    /**
     * 一个警告图标+一个标题+一句话+自定义按钮文字和事件,输入框
     *
     * @param activity
     * @param titleText
     * @param contentText
     * @param cancelable
     * @param touchOutSideToCancel
     * @param cancelListener
     * @param confirmListener
     */
    public void showDialogByEditText(GBaseActivity activity, String titleText, String contentText, boolean cancelable, boolean touchOutSideToCancel, OnSweetClickListener cancelListener, OnSweetClickListener confirmListener, String CancelText, String ConfirmText) {
        if (activity == null) return;
        if (dialog != null && dialog.isShowing()) {
            // progressDlg.setMsg(sMsg);
        } else {
            if (!activity.isFinishing() && !activity.isOnStopActivity) {
                dialog = new SweetAlertDialog(activity, SweetAlertDialog.EDITTEXT_TYPE);
                dialog.setCancelable(cancelable);
                dialog.setTitleText(titleText);
//				dialog.setContentText(contentText);
                dialog.setCancelText(CancelText);
                dialog.setConfirmText(ConfirmText);
//				dialog.setTipText(contentText);
                dialog.setCanceledOnTouchOutside(touchOutSideToCancel);
                dialog.setCancelClickListener(cancelListener);
                dialog.setConfirmClickListener(confirmListener);
                dialog.show();
            }
        }

    }

    /**
     * 一个自定义图标+一个标题+一句话
     *
     * @param activity
     * @param titleText
     * @param contentText
     * @param cancelable
     * @param touchOutSideToCancel
     * @param drawble
     */
    public void showDialogByCustomImage(GBaseActivity activity, String titleText, String contentText, boolean cancelable, boolean touchOutSideToCancel, int drawble) {
        if (activity == null) return;
        if (dialog != null && dialog.isShowing()) {
            // progressDlg.setMsg(sMsg);
        } else {
            if (!activity.isFinishing() && !activity.isOnStopActivity) {
                dialog = new SweetAlertDialog(activity, SweetAlertDialog.CUSTOM_IMAGE_TYPE);
                dialog.setCancelable(cancelable);
                dialog.setTitleText(titleText);
                dialog.setContentText(contentText);
                dialog.setCanceledOnTouchOutside(touchOutSideToCancel);
                dialog.setCustomImage(drawble);
                dialog.show();
            }
        }
    }

    private static class CountDownTimerClass extends CountDownTimer {

        SweetAlertDialog dialog;
        GBaseActivity activity;
        private int i = -1;

        public CountDownTimerClass(long millisInFuture, long countDownInterval, SweetAlertDialog sweetAlertDialog_, GBaseActivity activity_) {
            super(millisInFuture, countDownInterval);
            dialog = sweetAlertDialog_;
            activity = activity_;
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onTick(long millisUntilFinished) {
            i++;
            if (i == 7) {
                i = 0;
            }
            switch (i) {
                case 0:
                    dialog.getProgressHelper().setBarColor(activity.getResources().getColor(R.color.blue_btn_bg_color));
                    break;
                case 1:
                    dialog.getProgressHelper().setBarColor(activity.getResources().getColor(R.color.material_deep_teal_50));
                    break;
                case 2:
                    dialog.getProgressHelper().setBarColor(activity.getResources().getColor(R.color.success_stroke_color));
                    break;
                case 3:
                    dialog.getProgressHelper().setBarColor(activity.getResources().getColor(R.color.material_deep_teal_20));
                    break;
                case 4:
                    dialog.getProgressHelper().setBarColor(activity.getResources().getColor(R.color.material_blue_grey_80));
                    break;
                case 5:
                    dialog.getProgressHelper().setBarColor(activity.getResources().getColor(R.color.warning_stroke_color));
                    break;
                case 6:
                    dialog.getProgressHelper().setBarColor(activity.getResources().getColor(R.color.success_stroke_color));
                    break;
            }
        }

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub

        }

    }

    /**
     * 加载进度条
     *
     * @param activity
     * @param contentText
     */
    public void showDialogByProgress(final GBaseActivity activity, String contentText) {
        if (activity == null) return;
        if (dialog != null && dialog.isShowing()) {
            // progressDlg.setMsg(sMsg);
        } else {
            if (!activity.isFinishing() && !activity.isOnStopActivity) {

                dialog = new SweetAlertDialog(activity, SweetAlertDialog.PROGRESS_TYPE);
                dialog.setCancelable(true);
                dialog.setTitleText(contentText);
                dialog.setCanceledOnTouchOutside(false);
                dialog.show();
                new CountDownTimerClass(800 * 20, 800, dialog, activity).start();
         /*new CountDownTimer(800 * 20, 800) {
             public void onTick(long millisUntilFinished) {
                 // you can change the progress bar color by ProgressHelper every 800 millis
                 i++;
                 if (i==7) {
						i=0;
					}
                 switch (i){
                     case 0:
                    	 dialog.getProgressHelper().setBarColor(activity.getResources().getColor(R.color.blue_btn_bg_color));
                         break;
                     case 1:
                    	 dialog.getProgressHelper().setBarColor(activity.getResources().getColor(R.color.material_deep_teal_50));
                         break;
                     case 2:
                    	 dialog.getProgressHelper().setBarColor(activity.getResources().getColor(R.color.success_stroke_color));
                         break;
                     case 3:
                    	 dialog.getProgressHelper().setBarColor(activity.getResources().getColor(R.color.material_deep_teal_20));
                         break;
                     case 4:
                    	 dialog.getProgressHelper().setBarColor(activity.getResources().getColor(R.color.material_blue_grey_80));
                         break;
                     case 5:
                    	 dialog.getProgressHelper().setBarColor(activity.getResources().getColor(R.color.warning_stroke_color));
                         break;
                     case 6:
                    	 dialog.getProgressHelper().setBarColor(activity.getResources().getColor(R.color.success_stroke_color));
                         break;
                 }
             }
             //加载失败
             public void onFinish() {
                 i = -1;

                 sweetAlertDialog.setTitleTextText("网络异常")
                         .setConfirmText("error")
                         .changeAlertType(SweetAlertDialog.ERROR_TYPE);
             }
         }.start();*/
            }
        }
    }

    /**
     * 关闭加载框
     */
    public void dismissSweetDialog(GBaseActivity activity) {
        if (activity == null) return;
        try {
            if (dialog != null && dialog.isShowing()
                    && !activity.isFinishing() && !isSuccessNotCancel() && !activity.isOnStopActivity) {
                //dialog.dismissWithAnimation();
                dialog.dismiss();
            }
        } catch (Exception e) {
            AbLogUtil.e("dismissSweetDialog", e.getMessage());
        }


    }

    public void showDialogByUpdateApk(GBaseActivity activity, String titleText, String contentText, boolean cancelable, boolean touchOutSideToCancel, OnSweetClickListener cancelListener, OnSweetClickListener confirmListener) {
        if (activity == null) return;

        SweetAlertDialog dialog = new SweetAlertDialog(activity, SweetAlertDialog.UpdateApp_TYPE);
      //  dialog.getWarning_frame().setVisibility(View.GONE);
                dialog.setCancelable(cancelable);
                dialog.setTitleText(titleText);
                dialog.setContentText(contentText);
                dialog.setCancelText("取消");
                dialog.setConfirmText("确定");
                dialog.setCanceledOnTouchOutside(touchOutSideToCancel);
                dialog.setCancelClickListener(cancelListener);
                dialog.setConfirmClickListener(confirmListener);
                dialog.show();

        }




}
